<?php

namespace App\Admin\Controllers;

use App\Models\Base\LogVip;
use App\Models\Base\ServerList;
use App\Models\Game\PlayerGamedata;
use App\Service\GameService;
use Encore\Admin\Controllers\ModelForm;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Input;

class AddVipLevelController extends Controller
{
    use ModelForm;

    private static $header ;

    public function __construct(Request $request)
    {
        parent::__construct($request);
        self::$header = trans('game.gm_tool');
    }

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {
            $content->header(self::$header);
            $content->description(trans('game.add_vip_lv'));

            $content->body($this->grid());
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     *
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {
            $content->header(self::$header);
            $content->description(trans('admin.edit'));

            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {
            $content->header(self::$header);
            $content->description(trans('admin.create'));

            $content->body($this->form());
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        ServerList::$soft = true;

        return Admin::grid(LogVip::class, function (Grid $grid) {
            $grid->model()->orderBy('id', 'desc');
            $grid->id('ID')->sortable();
            $grid->serverName()->name(trans('game.server_name'));
            $grid->recipient_type(trans('game.type'))->display(function ($type) {
                return $type ? trans('game.all_people') : trans('game.specified');
            });
            $grid->level_section(trans('game.level_interval'));
            $grid->recipients(trans('game.recipient'));
            $grid->fairy_num(trans('game.crystal_number'));
            $grid->integral_num(trans('game.integral_number'));
            $grid->created_at(trans('admin.created_at'));
            $grid->disableFilter();
            $grid->disableActions();
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Admin::form(LogVip::class, function (Form $form) {
            $form->display('id', 'ID');
            $form->select('server_id', trans('game.server_name'))->options(ServerList::all()->pluck('name', 'server_id'))->rules('required|min:1|numeric');
            $form->radio('recipient_type', trans('game.type'))->options([0 => trans('game.specified'), 1 => trans('game.all_people') ])->default(1);
            $form->text('level_section', trans('game.level_interval'))->attribute(['id' => 'level_section'])->help('格式为xx-xx');
            $form->multipleSelect('recipients', trans('game.recipient'))->attribute(['id' => 'recipients'])->ajaxLoad('server_id', route('api.player'));
            $form->textarea('content', trans('game.note'))->rows(2)->rules('required')->rules('required|min:5');
            $form->text('fairy_num', trans('game.crystal_number'))->rules('required|numeric')->default(0);
            $form->text('integral_num', trans('game.integral_number'))->rules('required|numeric')->default(0);
            $form->display('created_at', trans('game.created_at'));
            $form->display('updated_at', trans('game.updated_at'));
            $form->datetime('exp_time', trans('game.exp_time'))->help(trans('game.permanent'));
            $this->roleScript();
            $form->saving(function ($form) {
                if (!$this->sendGS()) {
                    return back()->with(admin_toastr(trans('game.gs_communication_failed'), 'error'))->withInput();
                }
            });
        });
    }

    //加仙晶发送给游戏服
    private function sendGS()
    {
        $post = Input::post();
        $time = $post['exp_time'] ? strtotime($post['exp_time']) : 0;
        $exp_time = $time < time() ? 0 : $time;
        $server_id = $post['server_id'];
        $recipients = $post['recipients'];
        $fairy_num = $post['fairy_num']; //仙晶数量
        $integral_num = $post['integral_num']; //积分数量
        $type = 4;
        if ($post['level_section']) {
            $level = explode('-', $post['level_section']);
            $min_level = $level[0];
            $max_level = $level[1];
        } else {
            $min_level = $max_level = 0;
        }

        if ($post['recipient_type']) {
            //全服
            $role_name = '__ALLTHEROLE__';
            $ret = GameService::addVip($server_id, $role_name, $fairy_num, $integral_num, $min_level, $max_level, $exp_time,1);
            if (!$ret) {
                return false;
            }
        } else {
            //指定
            if (empty($recipients)) {
                return false;
            } else {
                $identy_id = 1;
                setDB($server_id);
                foreach ($recipients as $player_id) {
                    if (null == $player_id) {
                        continue;
                    }
                    $play = PlayerGamedata::find($player_id);
                    $role_name = $play->name;
                    $ret = GameService::addVip($server_id, $role_name, $fairy_num, $integral_num, $min_level, $max_level,0,0);
                    if (!$ret) {
                        return false;
                    }
                }
            }
        }

        return true;
    }
}
